/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.homesoft.component;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author phucan
 */
public class HmSuggestedComboDb extends JPanel implements Serializable {

    JComboBox cmbHint = new JComboBox();
    JTextField txtInput;
    private boolean hide_flag;
    SuggestedQuery queryObject;
    SuggestedComboBoxModel model;
    int top = 10;
    boolean matchStart = false;

    public HmSuggestedComboDb() {
        model = new SuggestedComboBoxModel();
        cmbHint.setEditable(true);
        txtInput = (JTextField) cmbHint.getEditor().getEditorComponent();
        this.setLayout(new BorderLayout());

        this.add(cmbHint, BorderLayout.NORTH);
        txtInput.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                final int code = e.getKeyCode();
                EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        String text = txtInput.getText();
                        if (text.length() == 0) {
                            cmbHint.hidePopup();
                            // setModel(new DefaultComboBoxModel(v), "");
                            if (model.list != null) {
                                model.list.clear();
                                model.setSelectedItem(null);
                            }
                            // cmbHint.setModel(model);
                        } else {
                            //SuggestedComboBoxModel m = getSuggestedModel(v, text);
                            if (code != KeyEvent.VK_ENTER) {
                                updateModel(text);
                                if (model.list.isEmpty() || hide_flag) {
                                    cmbHint.hidePopup();
                                    hide_flag = false;
                                } else {
                                    //setModel(m, text);                                                                                         
                                    cmbHint.showPopup();
                                }
                            }
                        }
                    }

                });
            }

            @Override
            public void keyPressed(KeyEvent e) {
                String text = txtInput.getText();
                int code = e.getKeyCode();
                if (code == KeyEvent.VK_ENTER) {
                    // if (!v.contains(text)) {
                    //  v.addElement(text);
                    //    Collections.sort(v);
                    // setModel(getSuggestedModel(text), text);
                    //  }
                    if (cmbHint.getSelectedItem() != null) {
                        txtInput.setText(cmbHint.getSelectedItem().toString());
                    }
                    hide_flag = true;
                } else if (code == KeyEvent.VK_ESCAPE) {
                    hide_flag = true;
                }
//                else if (code == KeyEvent.VK_RIGHT) {
//                    for (int i = 0; i < model.list.size(); i++) {
//                        String str = model.list.get(i).toString();
//                        if (str.contains(text)) {
//                            cmbHint.setSelectedIndex(-1);
//                            txtInput.setText(str);
//                            return;
//                        }
//                    }
//                }
            }
        });
        cmbHint.addKeyListener(null);
        cmbHint.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                //super.keyPressed(e); //To change body of generated methods, choose Tools | Templates.
                if (cmbHint.getSelectedItem() != null) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        txtInput.setText(cmbHint.getSelectedItem().toString());
                    }
                }
            }

        });

    }

    public HmSuggestedComboDb(SuggestedQuery qo) {
        this();
        this.queryObject = qo;

    }

    public void addItemListener(ItemListener evt) {
        cmbHint.addItemListener(evt);
    }

    private void updateModel(String str) {
        // cmbHint.setModel(mdl);
        List data = queryObject.fetchData(str, top, matchStart);
        // List<ListItem> items=new ArrayList<>();
//        for(Object o : data){
//            items.add(new ListItem(o.toString(), o));
//        }
        model = new SuggestedComboBoxModel();

        model.setData(data);

        cmbHint.setModel(model);
        cmbHint.setSelectedIndex(-1);
        //cmbHint.updateUI();
        txtInput.setText(str);
    }

//    private SuggestedComboBoxModel getSuggestedModel(String text) {
//        List list=queryObject.fetchData(text, top, matchStart);
//        return null;
//    }
    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public boolean isMatchStart() {
        return matchStart;
    }

    public void setMatchStart(boolean matchStart) {
        this.matchStart = matchStart;
    }

    public void setQueryObject(SuggestedQuery queryObject) {
        this.queryObject = queryObject;
    }

    public String getText() {
        return txtInput.getText();
    }

    public int getSelectedIndex() {
        return cmbHint.getSelectedIndex();
    }

    public Object getSelectedItem() {
        return cmbHint.getSelectedItem();
    }

    public void setText(String text) {
        txtInput.setText(text);
    }
}
